//
//  MyScene.m
//  Legions
//
//  Created by Anders Petersen on 05/05/14.
//  Copyright (c) 2014 Anders Petersen. All rights reserved.
//

#import "MyScene.h"
#import "Army.h"
#import "GameObject.h"
#import "Button.h"

@interface MyScene()
@end

SKNode          *worldNode;
Army *army1;
Army *army2;
Army *army3;
Army *army4;

GameObject *selectedObject;

CGPoint worldNodeTransLation;
CGFloat worldNodeScale = 1;


@implementation MyScene



-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {

        self.backgroundColor        = [SKColor colorWithRed:.0 green:.3 blue:.0 alpha:1];;
        
        //world
        worldNode                   = [SKNode node];
        [worldNode setPosition:CGPointMake(0, 0)];
        [self addChild:worldNode];

        //scene game objects
        army1                        = [[Army alloc] init];
        [army1 setPosition:CGPointMake(100, 50)];
        army2                        = [[Army alloc] init];
        [army2 setPosition:CGPointMake(100, 200)];
        army3                        = [[Army alloc] init];
        [army3 setPosition:CGPointMake(100, 300)];
        army4                        = [[Army alloc] init];
        [army4 setPosition:CGPointMake(100, 400)];
        
        [worldNode addChild:army1];
        [worldNode addChild:army2];
        [worldNode addChild:army3];
        [worldNode addChild:army4];
        
        [self addCurveToWorldNode: -500 and: -500];
        [self addCurveToWorldNode: -300 and: -300];
        
        
        //physics
        self.physicsWorld.gravity   = CGVectorMake(0,0);
        
        worldNodeTransLation = CGPointMake(0, 0);
    }
    return self;
}
 
- (void) addCurveToWorldNode: (float) xStart and: (float) xEnd{
    CGMutablePathRef cgpath  = CGPathCreateMutable();
    
    
    //ControlPoint1
    float cp1X = 0;
    float cp1Y = 100;
    
    //ControlPoint2
    float cp2X = 200;
    float cp2Y = 300;
    
    CGPoint s = CGPointMake(xStart, 500.0);
    CGPoint e = CGPointMake(xEnd, -500.0);
    CGPoint cp1 = CGPointMake(cp1X, cp1Y);
    CGPoint cp2 = CGPointMake(cp2X, cp2Y);
    CGPathMoveToPoint(cgpath,NULL, s.x, s.y);
    CGPathAddCurveToPoint(cgpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
        
    
    SKShapeNode *ball = [[SKShapeNode alloc] init];
    
    //        CGMutablePathRef myPath = CGPathCreateMutable();
    //        CGPathAddArc(myPath, NULL, 0,0, 15, 0, M_PI*2, YES);
    
    
    ball.lineWidth = 0;
    ball.fillColor = [SKColor colorWithRed:0 green:0 blue:0 alpha:.15];
    ball.strokeColor = [SKColor whiteColor];
    ball.glowWidth = 0.0;
    
    
    
    [ball setPosition:CGPointMake(200, 100)];
    
    ball.path = cgpath;

    
    [worldNode addChild:ball];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    for (UITouch *touch in touches) {
        
        SKNode *node = [self nodeAtPoint:[touch locationInNode:self]];
        
        if(!selectedObject){
            if([node isKindOfClass: [GameObject class]])
            selectedObject = [((GameObject *)node) getGameObjectParent];            

        }else{
            CGPoint sceneCoordinates = [touch locationInNode:self];
                
            CGPoint worldCoordinates = [self convertPoint:sceneCoordinates toNode:worldNode];
            
                [selectedObject moveTo:worldCoordinates with:2];
                selectedObject = nil;            
        }
    }
}

-(void)update:(CFTimeInterval)currentTime {    
}


- (void)didBeginContact:(SKPhysicsContact *)contact
{
    SKPhysicsBody *firstBody, *secondBody;
    
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else
    {
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }
    if ((firstBody.categoryBitMask)) // & missileCategory) != 0)
    {
//        [self attack: secondBody.node withMissile:firstBody.node];
    }
}


- (void)didMoveToView:(SKView *)view {
    UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
    [[self view] addGestureRecognizer:gestureRecognizer];
    
    
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(twoFingerPinch:)];
    [[self view] addGestureRecognizer:pinch];
}


- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [recognizer translationInView:recognizer.view];
        translation = CGPointMake(translation.x, -translation.y);
    
        [worldNode setPosition:CGPointMake(translation.x + worldNodeTransLation.x , translation.y + worldNodeTransLation.y)];
    
    }else if (recognizer.state == UIGestureRecognizerStateEnded) {
    
        worldNodeTransLation.x  = worldNode.position.x;
        worldNodeTransLation.y  = worldNode.position.y;
    }
}

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
{
    CGFloat errorScale         = 1 - worldNodeScale;;
    
     if (recognizer.state == UIGestureRecognizerStateBegan) {
         
         
     }else if(recognizer.state == UIGestureRecognizerStateEnded){
          worldNodeScale = recognizer.scale - errorScale;
         
     }else if(recognizer.state == UIGestureRecognizerStateChanged){
         NSLog(@"Pinch scale: %f, %f", recognizer.scale, errorScale);
        
         [worldNode setScale: (recognizer.scale - errorScale)];
         
     }
}

@end
